package cn.lcsjsxy.yxz.day.Set;

import java.util.HashSet;

//HashSet重复数据
public class HashSetDuplicatedata {

    public static void main(String[] args) {

        //HashSet底层:数组 + 链表!
        HashSet<User2> set = new  HashSet();

        //
        User2 user1 = new User2();
        user1.id = 110;
        user1.name = "1";
        System.out.println(user1.hashCode());

//        User2 user2 = new User2();
//        user2.id = 110;
//        user2.name = "1";
//        System.out.println(user2.hashCode());

        User2 user3 = new User2();
        user3.id = 112;
        user3.name = "3";

        User2 user4 = new User2();
        user4.id = 110;
        user4.name = "2";
        System.out.println(user3.hashCode());

        set.add(user1);
//        set.add(user2);
        set.add(user3);
        set.add(user4);

        System.out.println(set);
    }

}

class User2{
    public int id;
    public String name;

    //
    @Override
    public String toString() {
        return "User[" + id + ", "+ name + "]";
    }

    //判断两个对象的属性是否相同
    @Override
    public boolean equals(Object obj) {
        if ( obj instanceof User2){
            User2 otherUser = (User2) obj;
            if (otherUser.id == this.id){
                if (otherUser.name.equals(this.name)){
                    return true;
                }
            }
            return false;
        }else {
            return false;
        }
    }

    //内存地址
    @Override
    public int hashCode() {
        return id;
    }
}